//
//  VisitorTableViewController.swift
//  Webo10
//
//  Created by ChengXiwu on 2017/7/28.
//  Copyright © 2017年 ChengXiwu. All rights reserved.
//

import UIKit

class VisitorTableViewController: UITableViewController {
    
    // 用户登录标记
    private var isLogon = UserAccountViewModel.userAccount.userIsLogon
    
    var visitorView: VisitorView?
    
    // 决定是否登录
    override func loadView() {
        logPrint("come to VisitorTableViewController")
        
        isLogon ? super.loadView() : setupVisitorView()
    }
    
    private func setupVisitorView(){
        // 替换根视图
        visitorView = VisitorView()
        view = visitorView
        
        // 添加监听方法
        visitorView?.registerButton.addTarget(self, action: #selector(visitorViewDidRegister), for: UIControlEvents.touchUpInside)
        visitorView?.loginButton.addTarget(self, action: #selector(visitorViewDidLogin), for: UIControlEvents.touchUpInside)
        
        // 设置导航栏按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(visitorViewDidRegister))
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .plain, target: self, action: #selector(visitorViewDidLogin))
        
    }
}

extension VisitorTableViewController{
    
    /// 注册方法
    func visitorViewDidRegister(){
        logPrint("注册")
        
        // 通过天气预报网站测试网络工具
//        NetworkTools.networkTools.request(method: HMRequestMethod.GET, url: "http://www.weather.com.cn/data/sk/101010100.html", parameters: nil) { (result, error) in
//            logPrint(result ?? "网络访问成功")
//        }
        
        // 通过测试网站http://httpbin.org/get或http://httpbin.org/post进行测试
//        NetworkTools.networkTools.request(method: HMRequestMethod.GET, url: "http://httpbin.org/get", parameters: ["name": "zhangsan"]) { (result, error) in
//            logPrint(result ?? "网络访问成功")
//        }
        NetworkTools.networkTools.sendRequest(method: HttpMethod.POST, url: "http://httpbin.org/post", parameters: ["name": "zhangsan"]) { (result, error) in
            logPrint(result ?? "网络访问成功")
        }

    }
    
    /// 登录方法
    func visitorViewDidLogin(){
        logPrint("登录")
        
        let vc = OAuthViewController()
        let nav = UINavigationController(rootViewController: vc)
        present(nav, animated: true, completion: nil)
    }
    
}
